package com.vancouver.knows.ui.activity.userpost;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.LinearLayout;

import com.pangu.util.StringUtil;
import com.vancouver.knows.R;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Chris Li on 2016-07-15.
 */
public class CreateUserPostCategoryView extends LinearLayout {

    public CreateUserPostCategoryView(Context context) {
        super(context);
        init();
    }

    public CreateUserPostCategoryView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CreateUserPostCategoryView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layoutInflater.inflate(R.layout.create_user_post_view, this, true);
        ButterKnife.bind(this);
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return true;
            }
        });
    }

    @OnClick({R.id.buy_and_sell, R.id.housing, R.id.mutual_support, R.id.jobs, R.id.friends_and_partners})
    public void onCategoryClicked(View view) {

        String chosenCategory = "";
        switch (view.getId()) {
            case R.id.buy_and_sell:
                chosenCategory = UserPostFilterActivity.BUY_AND_SELL_SLUG;
                break;
            case R.id.housing:
                chosenCategory = UserPostFilterActivity.HOUSING_SLUG;
                break;

            case R.id.mutual_support:
                chosenCategory = UserPostFilterActivity.MUTUAL_SUPPORT_SLUG;
                break;

            case R.id.jobs:
                chosenCategory = UserPostFilterActivity.JOBS_SLUG;
                break;

            case R.id.friends_and_partners:
                chosenCategory = UserPostFilterActivity.FRIENDS_AND_PARTNERS_SLUG;
                break;
        }

        if (!StringUtil.isEmptyString(chosenCategory)) {
            Intent intent = new Intent(getContext(), UserPostSubcategoryListActivity.class);
            intent.putExtra(UserPostSubcategoryListActivity.CATEGORY_EXTRA, chosenCategory);
            ((UserPostActivity) getContext()).startActivityForResult(intent, CreateUserPostActivity.CREATE_POST_SUCCESS_REQUEST);
        }
    }

    @Override
    public void setVisibility(int visibility) {

        if (visibility == VISIBLE) {
            Animation fadeIn = new AlphaAnimation(0, 1);
            fadeIn.setInterpolator(new DecelerateInterpolator());
            fadeIn.setDuration(300);
            startAnimation(fadeIn);
        } else if (visibility == GONE) {
            Animation fadeOut = new AlphaAnimation(1, 0);
            fadeOut.setInterpolator(new AccelerateInterpolator());
            fadeOut.setDuration(300);
            startAnimation(fadeOut);
        }

        super.setVisibility(visibility);
    }
}
